package ui.views;


import java.util.Calendar;

import org.eclipse.jface.action.MenuManager;
import org.eclipse.jface.viewers.TableViewer;
import org.eclipse.jface.viewers.TreeViewer;
import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Menu;
import org.eclipse.ui.part.ViewPart;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import atempdao.EgnProductDao;

import ui.control.SearchTextComposite;
import ui.filter.EntityFilter;
import ui.util.CalendarUtil;
import ui.util.IStructuredViewer;
import dao.EgnProduct;
import de.ralfebert.rcputils.builder.table.ColumnBuilder;
import de.ralfebert.rcputils.builder.table.TableViewerBuilder;
import de.ralfebert.rcputils.builder.table.format.Formatter;
import de.ralfebert.rcputils.properties.BaseValue;

public class ProductView extends ViewPart implements IStructuredViewer{
	private final static Logger logger = LoggerFactory.getLogger(ProductView.class);
	private TableViewer tableViewer;
//	private TableResultsTable tableResult;
	private EntityFilter filter;
	private SearchTextComposite swtComp;

	public ProductView() {
	}
	
	@Override
	public void createPartControl(Composite parent) {

		this.filter = new EntityFilter();
		
		swtComp = new SearchTextComposite(parent,SWT.NONE, filter);
		Composite comp = swtComp.getComp();
		
		createTable(comp);
//		Set Filter
		tableViewer.addFilter(filter);
		
		createContextMenu();
		
	}

	@Override
	public void setFocus() {
		// TODO Auto-generated method stub

	}
	@Override
	public TableViewer getTableViewer(){
		return tableViewer;
	}
	@Override
	public TreeViewer getTreeViewer(){
		return null;
	}
	private void createTable(Composite parent){
		TableViewerBuilder t = new TableViewerBuilder(parent);
		
		ColumnBuilder prodId = t.createColumn("Prod Id");
		prodId.bindToProperty("productId");
		prodId.useAsDefaultSortColumn();
		prodId.build();
		
		ColumnBuilder prodType = t.createColumn("Prod Type");
		prodType.bindToProperty("productType");
		prodType.build();
		
		ColumnBuilder currency = t.createColumn("Currency");
		currency.bindToProperty("currency");
		currency.build();
		
		ColumnBuilder issueDate = t.createColumn("Issue Date");
		issueDate.bindToValue(new BaseValue<EgnProduct>() {
			@Override
			public Object get(EgnProduct prod){
				Calendar cal = prod.getIssueDate();
				return CalendarUtil.toString(cal);
			}
		});
		issueDate.build();
		
		ColumnBuilder matDate = t.createColumn("Maturity Date");
		matDate.bindToValue(new BaseValue<EgnProduct>() {
			@Override
			public Object get(EgnProduct prod){
				Calendar cal = prod.getMaturityDate();
				return CalendarUtil.toString(cal);
			}
		});
		matDate.build();
		
		ColumnBuilder notional = t.createColumn("Notional");
		notional.bindToProperty("notional");
		notional.build();
		
		ColumnBuilder cntPartyId = t.createColumn("CountParty");
		cntPartyId.bindToProperty("cntPartyId");
		cntPartyId.build();
		
		ColumnBuilder creditGrade = t.createColumn("CreditGrade");
		creditGrade.bindToProperty("creditGrade");
		creditGrade.build();
		
		ColumnBuilder bizRule = t.createColumn("BIZ Rule");
		bizRule.bindToProperty("bizdayRule");
		bizRule.build();
		
		ColumnBuilder currentAmt = t.createColumn("Amount");
		currentAmt.bindToProperty("currentAmt");
		currentAmt.build();
		
		ColumnBuilder spotPrice = t.createColumn("Spot Price");
		spotPrice.bindToProperty("spotPrice");
		spotPrice.build();
		
		ColumnBuilder disCurve = t.createColumn("Discount Curve");
		disCurve.bindToProperty("discountCurve");
//		disCurve.setPercentWidth(10);
		disCurve.build();
		
		
		t.setInput(EgnProductDao.getAllEgnProduct(30));		
//		t.setInput(null);		
		tableViewer = t.getTableViewer();
		
		Formatter.setZebraBackground(tableViewer);

		logger.debug("Table Test:{},{}", tableViewer.getTable().getItem(0).getText(4),
				tableViewer.getTable().getColumn(0).getText());
		
//		logger.debug("Table Test2:{},{}", tableViewer.getTable().getColumns()[1],
//				tableViewer.getTable().));
//		logger.debug("Test Cell:{}", tableViewer.getTable().select(2));
		
	}
	
	private void createContextMenu() {
		MenuManager menuManager = new MenuManager();
//		menuManager.setRemoveAllWhenShown(true);
//		menuManager.addMenuListener(new IMenuListener() {
//			public void menuAboutToShow(IMenuManager mgr) {
//				fillContextMenu(mgr);
//			}
//		});
		Menu menu = menuManager.createContextMenu(tableViewer.getTable());
		// Set the MenuManager
		tableViewer.getTable().setMenu(menu);
		getSite().registerContextMenu(menuManager, tableViewer);
		// Make the selection available
		getSite().setSelectionProvider(tableViewer);
		
//		menuManager.add(new CopyTableAction());
		
	}
	
//	public void fillContextMenu(IMenuManager mgr){
//		Exporter expCSV = new ExporterCSV();
//		ExportAction action = new ExportAction(expCSV, tableResult);
//		mgr.add("net.sourceforge.sqlexplorer.sqleditor.results.actions.CopyTableAction");

//	}
	
}
